/**
 * @author Francesco di Dio
 * Date: 20/apr/2013
 * Titolo: AccountViewPanel.java
 * Versione: 
 */

/**
 * Copyright (c) 2013 Francesco di Dio.
 * tabuto83@gmail.com 
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */
package com.tabuto.jkeys.views;

import java.awt.Dimension;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.text.DefaultCaret;

import org.apache.log4j.Logger;

import com.tabuto.jkeys.Messages;
import com.tabuto.jkeys.controllers.JKControllerInterface;
import com.tabuto.jkeys.models.AbstractJKeyModel;
import com.tabuto.jkeys.models.Account;
import com.tabuto.jkeys.models.AccountGroup;

public class AccountViewPanel extends JPanel {
	
	private static final Logger log = Logger.getLogger(AccountViewPanel.class);
	/**
	 * 
	 */
	private static final long serialVersionUID = 3951725708238905127L;
	private JTextField title;
	private JTextField username;
	private JPasswordField password;
	private JTextArea txtrNotesArea;
	
	private JLabel lblTitle;
	private JLabel lblUsername;
	private JLabel lblPassword; 
	private JLabel lblUrl;
	private JLabel lblNotes;
	
	private JScrollPane scrollNotes;
	
	private JButton btnSave;

	private JButton btnShow;

	private JButton btnCopyUsername;
	private JButton btnCopyPassword;
	
	private JButton btnDelete;

	private JKControllerInterface jKeysController = null;
	private JTextField url;
	
	private boolean showPasswordMode = false;

	/**
	 * Create the panel.
	 */
	public AccountViewPanel(JKControllerInterface jKeysController) {
		super();
		this.setjKeysController(jKeysController);

	}

	private void initComponent() {
		lblTitle = new JLabel(Messages.getString("AccountViewPanel.lbl.title")); 
		lblTitle.setToolTipText(Messages.getString("AccountViewPanel.lbl.title")); 

		title = new JTextField();
		title.setToolTipText(Messages.getString("AccountViewPanel.lbl.alt_title")); 
		title.setColumns(10);

		lblUsername = new JLabel(Messages.getString("AccountViewPanel.lbl.username")); 

		username = new JTextField();
		username.setColumns(10);

		lblPassword = new JLabel(Messages.getString("AccountViewPanel.lbl.password")); 

		password = new JPasswordField();
		password.setVisible(!showPasswordMode); ;

		lblUrl = new JLabel(Messages.getString("AccountViewPanel.lbl.url")); //$NON-NLS-1$

		lblNotes = new JLabel(Messages.getString("AccountViewPanel.lbl.notes")); //$NON-NLS-1$

		txtrNotesArea = new JTextArea();
		DefaultCaret caret = (DefaultCaret) txtrNotesArea.getCaret();
		caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
		txtrNotesArea.setLineWrap(true);
		txtrNotesArea.setWrapStyleWord(true);

		scrollNotes = new JScrollPane(txtrNotesArea);
		scrollNotes.setPreferredSize(new Dimension(209, 50));

		btnSave = new JButton(Messages.getString("AccountViewPanel.lbl.save")); //$NON-NLS-1$
		btnSave.addActionListener(jKeysController.getSaveSingleAccountAction());

		btnShow = new JButton(Messages.getString("AccountViewPanel.lbl.show")); //$NON-NLS-1$
		btnShow.addActionListener(jKeysController.getShowPasswordAction());

		btnCopyUsername = new JButton(Messages.getString("AccountViewPanel.lbl.copy")); //$NON-NLS-1$
		btnCopyUsername.addActionListener(jKeysController
				.getCopyUsernameAction());

		btnCopyPassword = new JButton(Messages.getString("AccountViewPanel.lbl.copy")); //$NON-NLS-1$
		btnCopyPassword.addActionListener(jKeysController
				.getCopyPasswordAction());
		
		btnDelete = new JButton(Messages.getString("AccountViewPanel.lbl.delete")); 
		btnDelete.addActionListener(jKeysController.getDeleteSingleAccountAction());

		url = new JTextField();

		url.setColumns(10);
		
		
		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(lblUsername)
						.addComponent(lblPassword)
						.addComponent(lblTitle)
						.addComponent(lblNotes)
						.addComponent(lblUrl))
					.addGap(22)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
						.addComponent(scrollNotes, GroupLayout.DEFAULT_SIZE, 209, Short.MAX_VALUE)
						.addComponent(url)
						.addComponent(title)
						.addComponent(username, 164, 164, Short.MAX_VALUE)
						.addComponent(password)
						)
					.addGap(13)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(btnCopyPassword)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(btnShow, GroupLayout.PREFERRED_SIZE, 96, GroupLayout.PREFERRED_SIZE))
						.addComponent(btnCopyUsername)
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(btnSave)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(btnDelete)))
					.addContainerGap(18, Short.MAX_VALUE))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(45)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(title, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblTitle)
						.addComponent(btnSave)
						.addComponent(btnDelete))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblUsername)
						.addComponent(username, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(btnCopyUsername))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
							.addComponent(lblPassword)
							.addComponent(password, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							)
						.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
							.addComponent(btnCopyPassword)
							.addComponent(btnShow)))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(lblUrl)
						.addComponent(url, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(14)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(scrollNotes, GroupLayout.PREFERRED_SIZE, 103, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblNotes))
					.addContainerGap(26, Short.MAX_VALUE))
		);
		setLayout(groupLayout);
	}

	public JKControllerInterface getjKeysController() {
		return jKeysController;
	}

	public void setjKeysController(JKControllerInterface jKeysController) {
		this.jKeysController = jKeysController;
		initComponent();
	}

	public JTextField getTitle() {
		return title;
	}

	public JTextField getUsername() {
		return username;
	}

	public JPasswordField getPassword() {
		return password;
	}

	public JTextArea getTxtrNotesArea() {
		return txtrNotesArea;
	}

	public JTextField getUrl() {
		return url;
	}

	public void update() {
		
		log.debug("Refresh");
		
		AbstractJKeyModel selectedObject = jKeysController.getSelectedAccount();	
		boolean show = true;
		if (selectedObject instanceof Account) {
			Account selected = (Account) selectedObject;
			this.title.setText(selected.getName());
			this.username.setText(selected.getUsername());
			this.password.setText(selected.getPassword());
			//this.passwordShown.setText(selected.getPassword());
			this.url.setText(selected.getUrl());
			this.txtrNotesArea.setText(selected.getNotes());
			this.password.setEchoChar((showPasswordMode)?(char)0:'•' );
			show = true;
			
		} else if (selectedObject instanceof AccountGroup) {
			AccountGroup selected = (AccountGroup) selectedObject;
			this.title.setText(selected.getName());
			show = false;			
		}
		
		this.username.setVisible(show);
		this.url.setVisible(show);
		this.password.setVisible(show);
		this.scrollNotes.setVisible(show);
		this.txtrNotesArea.setVisible(show);
		
		this.lblUsername.setVisible(show);
		this.lblPassword.setVisible(show);
		this.lblUrl.setVisible(show);
		this.lblNotes.setVisible(show);
		
		
		this.btnShow.setVisible(show);
		this.btnCopyUsername.setVisible(show);
		this.btnCopyPassword.setVisible(show);
		this.repaint();
	}

	public void switchShowPasswordMode(){
		this.showPasswordMode = !showPasswordMode;
		
	}

	@Override
	public String toString() {
		return "AccountViewPanel [title=" + title.getText() + ", username=" + username.getText()
				+ ", password=" + password.isVisible()
				+ ", txtrNotesArea=" + txtrNotesArea.getText() + ", url=" + url.getText()
				+ ", showPasswordMode=" + showPasswordMode + "]";
	}
	
	
}
